﻿using Hidistro.Core;
using Hidistro.Entities.Orders;
using Hidistro.SaleSystem.Shopping;
using Hidistro.UI.Common.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using wnxd.javascript;
using System.Web.UI.HtmlControls;

namespace Hidistro.UI.SaleSystem.CodeBehind
{
    public class OrderCx : HtmlTemplatedWebControl
    {

        private Literal OrderId;
        private Literal kdgs;
        private Literal ydh;
        private Literal sjr;
        private Literal shdz;
        private Literal lxfs;
        private Literal cptp;
        private Literal cpmc;
        private Literal jg;
        private Literal dzzt;
        private Literal xdsj;
        private Literal cpjg;
        private Literal cpzj;
        private Literal wlxx;
        private Literal yb;
        private IButton cc;
        private TextBox txt;
        private HtmlGenericControl info;
        private HtmlGenericControl err;
        protected override void AttachChildControls()
        {

            this.OrderId = (Literal)this.FindControl("OrderId");
            this.kdgs = (Literal)this.FindControl("kdgs");
            this.ydh = (Literal)this.FindControl("ydh");
            this.sjr = (Literal)this.FindControl("sjr");
            this.shdz = (Literal)this.FindControl("shdz");
            this.lxfs = (Literal)this.FindControl("lxfs");
            this.cptp = (Literal)this.FindControl("cptp");
            this.cpmc = (Literal)this.FindControl("cpmc");
            this.jg = (Literal)this.FindControl("jg");
            this.dzzt = (Literal)this.FindControl("dzzt");
            this.xdsj = (Literal)this.FindControl("xdsj");
            this.cpjg = (Literal)this.FindControl("cpjg");
            this.cpzj = (Literal)this.FindControl("cpzj");
            this.wlxx = (Literal)this.FindControl("wlxx");
            this.cc = ButtonManager.Create(this.FindControl("cc"));
            this.cc.Click += new EventHandler(this.cc_Click);
            this.txt = (TextBox)this.FindControl("txt");
            this.yb = (Literal)this.FindControl("yb");
            this.wlxx = (Literal)this.FindControl("wlxx");
            this.info = (HtmlGenericControl)this.FindControl("info");
            this.err = (HtmlGenericControl)this.FindControl("err");
            this.info.Visible = false;
            this.err.Visible = false;
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-OrderCx.html";
            }
            base.OnInit(e);
        }
        protected void cc_Click(object sender, EventArgs e)
        {
            string orderId = this.txt.Text.Trim();
            OrderInfo orderInfo = ShoppingProcessor.GetOrderInfo(orderId);
            if (orderInfo != null)
            {
                this.info.Visible = true;
                this.err.Visible = false;
                OrderId.Text = orderInfo.OrderId;
                kdgs.Text = orderInfo.ExpressCompanyName == null ? "暂未知" : orderInfo.ExpressCompanyName;
                ydh.Text = orderInfo.ShipOrderNumber == null ? "暂未知" : orderInfo.ShipOrderNumber;
                sjr.Text = orderInfo.ShipTo;
                shdz.Text = orderInfo.ShippingRegion + " " + orderInfo.Address;
                lxfs.Text = orderInfo.CellPhone;
                dzzt.Text = OrderInfo.GetOrderStatusName((OrderStatus)orderInfo.OrderStatus);
                xdsj.Text = orderInfo.OrderDate.ToString();
                cpjg.Text = orderInfo.GetTotal().ToString();
                cpzj.Text = orderInfo.GetTotal().ToString();
                jg.Text = orderInfo.GetTotal().ToString();
                yb.Text = orderInfo.ZipCode == null ? "暂未知" : orderInfo.ZipCode; ;
                foreach (var v in orderInfo.LineItems)
                {
                    cptp.Text = " <img src=http://image.juseshop.com" + v.Value.ThumbnailsUrl160 + " width=\"120\" height=\"120\" />";
                    cpmc.Text = v.Value.ItemDescription;
                }
                string wlxxstr = "<ul>";
                string rstr = SearchExpressData(orderInfo);
                if (rstr == "商品正在出库")
                {
                    wlxxstr += "<li><p>" + orderInfo.OrderDate.ToString() + "</p><span>正在出库</span>";
                }
                else
                {

                    json jn = new json(SearchExpressData(orderInfo));
                    json data = jn["data"];
                    for (int i = 0; i < data.length; i++)
                    {
                        wlxxstr += " <li><p>" + (string)data[i].time + "</p><span>" + (string)data[i].context + "</span></li>";
                    }

                    wlxxstr += "</ul>";
                }

                wlxx.Text = wlxxstr;
            }
            else
            {
                this.err.Visible = true;
                this.info.Visible = false;
            }
        }
        private string SearchExpressData(OrderInfo orderInfo)
        {
            string s = string.Empty;

            if (((orderInfo != null) && ((orderInfo.OrderStatus == OrderStatus.SellerAlreadySent) || (orderInfo.OrderStatus == OrderStatus.Finished))) && !string.IsNullOrEmpty(orderInfo.ExpressCompanyAbb))
            {
                s = Express.GetExpressData(orderInfo.ExpressCompanyAbb, orderInfo.ShipOrderNumber);
            }
            else
            {
                s = "商品正在出库";
            }

            return s;
        }
    }
}